import java.io.*;
import java.net.Socket;

public class ClientDemo {
    public static void main(String[] args) {

        //创建客户端的Socket
        Socket socket = null;
        try {
            //1.创建Socket对象，指明服务器IP和端口号
            socket = new Socket("127.0.0.1", 10010);
            //创建BufferedReader
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            //读取一行
            String line = null;
            while ((line = br.readLine()) != null) {//null只是作为 一个文件是否读完
                if ("over".equals(line)) {
                    break;
                }
                //2.获取一个输出流用于输出数据
                OutputStream outputStream = socket.getOutputStream();
                //3.写出数据的操作(这里也可以用转换流OutputStreamWriter)
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
                BufferedWriter bw = new BufferedWriter(outputStreamWriter);
                //给封装的通道内的流对象bw写一行
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
